package com.bicon.botu.mall.member.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.mall.member.dto.AttentionReconmmendDTO;
import com.bicon.botu.mall.member.service.IAttentionService;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.member.common.MemberConstants;
import com.bicon.botu.mall.member.entity.Attention;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 关注功能 客户端接口
 *
 * @Author 李双喜
 * @Date 2017/7/22 13:19
 */
@Slf4j
@RestController
@RequestMapping("/V1.0.0/api/attention")
public class AttentionApi extends BaseController {
    @Autowired
    private IAttentionService iAttentionService;

    /**
     * 批量添加关注--商品详情
     * @return BaseResult
     */
    @RequestMapping(value = "/addAttention", method = RequestMethod.POST)
    public BaseResult addAttention(@RequestParam("attentionJson") String attentionJson) {
        String result= iAttentionService.insertAttention(getUserId(),attentionJson);
        if (!"".equals(result)){
            return sendResult200(result);
        }
        return sendResult500();
    }

    /**
     * 取消关注(根据id删除)----商品详情
     *
     * @param id 关注id
     * @return
     */
    @RequestMapping(value = "/deleteAttentionById", method = RequestMethod.POST)
    public BaseResult deleteAttentionById(@RequestParam String id) {
        Attention attention = iAttentionService.selectOne(new EntityWrapper<Attention>().eq("id", id).eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        attention.setLogicDelete(Constants.DEL_LOGIC_DELETE);
        attention.preUpdate();
        boolean result = iAttentionService.updateById(attention);
        if (result) {
            return sendResult(MemberConstants.RESPONSE_CODE_200, MemberConstants.RESPONSE_MESSAGE_200);
        } else {
            return sendResult(MemberConstants.RESPONSE_CODE_600, MemberConstants.RESPONSE_MESSAGE_600);
        }
    }

    /**
     * 我的关注排序（根据userId查询）
     * 默认排序,降价排序，促销排序
     * @param userId 用户id
     * @return BaseResult
     */
    @RequestMapping(value = "/selectAllAttention", method = RequestMethod.POST)
    public BaseResult selectAllAttention(@RequestParam("userId") String userId) {
        return iAttentionService.selectDefaultAttention(userId);
    }

    /**
     * 查询所有关注商品
     * @return BaseResult
     */
    @RequestMapping(value = "/selectAttention", method = RequestMethod.POST)
    public BaseResult selectAttention() {
        return iAttentionService.selectAttention(getUserId());
    }


    /**
     * 我的关注  默认下，取消关注的（根据Id的逻辑删除）  批量删除
     *
     * @param attentionIdJson 多个关注id
     * @return
     */
    @RequestMapping(value = "/deleteAttention", method = RequestMethod.POST)
    public BaseResult deleteAttention(@RequestParam("attentionIdJson") String attentionIdJson) {
        return iAttentionService.deleteAttention(attentionIdJson);
    }

    /**
     * 看相似（根据商品分类相似和价格相似查询）
     * @param categoryId 类目id
     * @param price      库存价格
     * @param pageNo     当前页码
     * @param pageSize   每页显示条数
     * @return page
     */
    @RequestMapping(value = "selectResemble", method = RequestMethod.POST)
    public BaseResult selectResemble(@RequestParam("categoryId") String categoryId, @RequestParam("price") BigDecimal price,
                                     @RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        Page<AttentionReconmmendDTO> page = iAttentionService.selectResemble(categoryId, price, pageNo, pageSize);
        if (null != page) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, page);
        }
        return new BaseResult(Constants.RESPONSE_CODE_602, Constants.RESPONSE_MESSAGE_602);
    }

    /**
     * 关注：为你推荐
     * @param categoryIdJson     多个商品类目id
     * @return
     */
    @RequestMapping(value = "selectRecommend",method = RequestMethod.POST)
    public BaseResult selectRecommend (@RequestParam("categoryIdJson") String categoryIdJson) {
        return iAttentionService.selectAttentionRecommend(categoryIdJson);
    }

    /**
     * 查询该商品是否被关注
     * @param userId   用户Id
     * @param productId 商品Id
     * @return
     */
    @RequestMapping(value = "/selectOneAttention",method = RequestMethod.POST)
    public BaseResult selectOneAttention(@RequestParam("userId") String userId,@RequestParam("productId") String productId){
        Attention attention=  iAttentionService.selectOne(new EntityWrapper<Attention>().eq("user_id",userId).eq("product_id",productId).eq("logic_delete",Constants.DEL_LOGIC_NORMAL));
        if(null==attention){
            return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200);
        }
        return sendResult(Constants.RESPONSE_CODE_200,Constants.RESPONSE_MESSAGE_200,attention.getId());
    }
}

